package cn.hiapi.shop.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.user.IUserService;
import cn.hiapi.shop.entity.order.OrderComment;
import cn.hiapi.shop.query.CommentQuery;
import cn.hiapi.shop.request.CommentReplyRequest;
import cn.hiapi.shop.service.order.CommentRequest;
import cn.hiapi.shop.service.order.OrderCommentService;
import cn.hiapi.shop.service.shop.ShopLinkUserService;
import cn.hiapi.shop.vo.CommentVo;
import cn.hiapi.shop.vo.OrderReplayJsonVo;
import cn.hiapi.shopkeeper.entity.order.ActivityOrder;
import cn.hiapi.shopkeeper.entity.order.CommentReplay;
import cn.hiapi.shopkeeper.service.order.ActivityOrderService;
import cn.hiapi.shopkeeper.service.order.CommentReplayService;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2023/1/31 0:23
 */
@RestController("shopkeeperCommentController")
@RequestMapping("/shop/comment")
public class CommentController extends BasicQueryController<OrderComment, Long, CommentVo, CommentQuery> {
    protected CommentController(OrderCommentService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }


    @Override
    public CommentQuery setQuery(CommentQuery query) {
        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(TokenGet.get().getFid(), query.getShopId());
        return super.setQuery(query);
    }

    @Override
    public List<CommentVo> toListVo(List<OrderComment> list, String[] fields) {
        return super.toList(list, fields);
    }

    @Override
    public CommentVo toData(OrderComment data, String[] fields) {
        CommentVo vo = data.copy(new CommentVo());
        vo.setUser(this.dispatchContext.getServiceOne(IUserService.class).fieldBase(data.getBuyerId()));
        vo.setThumbs(StringUtils.split(data.getImagesText(), ","));
        if (vo.getThumbs() == null) vo.setThumbs(new String[]{});
        if (data.getReplyStatus() == 1) {
            List<OrderComment> comments = basicService.findList(new QueryWrapper().eq("pid", data.getId()));
            List<CommentVo> collect = comments.stream().map(e -> {
                CommentVo copy = e.copy(new CommentVo());
                copy.setThumbs(StringUtils.split(e.getImagesText(), ","));
                if (copy.getThumbs() == null) copy.setThumbs(new String[]{});
                return copy;
            }).collect(Collectors.toList());
            vo.setReplyList(collect);
        }

        ActivityOrder order = this.dispatchContext.getServiceOne(ActivityOrderService.class).get(data.getOid());
        if (order != null) {
            JSONObject build = JsonUtils.builder()
                    .put("title", order.getTitle())
                    .put("thumb", order.getThumb())
                    .put("price", order.getPrice())
                    .build();

            vo.setOrder(build);

        }
        return vo;
    }


    @ApiOperation(value = "商家回复评论", notes = "商家回复评论", nickname = "AdinZ")
    @PostMapping("")
    public ResponseEntity submit(@RequestBody CommentReplyRequest request) {

        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(TokenGet.get().getFid(), request.getShopId());

        OrderComment comment = basicService.get(request.getId());
        if (comment == null || comment.getShopId() != request.getShopId()) return toError("回复评论不存在");

        if (comment.getReplyStatus() != 0) return toError("已经回复过了");

        comment.setReplyStatus(1);
        comment.setUpdated(System.currentTimeMillis());


        OrderComment reply = new OrderComment();
        reply.setPid(comment.getId());
        reply.setBody(request.getBody());
        reply.setImagesText(StringUtils.join(request.getThumbs(), ","));
        reply.setReply(1);
        reply.setGid(comment.getGid());
        reply.setShopId(comment.getShopId());
        reply.setOid(comment.getOid());

        basicService.save(comment);
        basicService.save(reply);

        CommentReplay commentReplay = new CommentReplay();
        commentReplay.setReplayId(reply.getId());
        commentReplay.setUid(comment.getBuyerId());

        OrderReplayJsonVo json = new OrderReplayJsonVo();
        json.setShopId(comment.getShopId());
        json.setBody(reply.getBody());
        json.setThumbs(request.getThumbs());
        json.setCreated(reply.getCreated());

        CommentVo commentVo = new CommentVo();
        commentVo.setBody(comment.getBody());
        commentVo.setThumbs(StringUtils.split(comment.getImagesText(), ","));
        json.setComment(commentVo);
        commentReplay.setDataJson(json.toJSONString());
        this.dispatchContext.getServiceOne(CommentReplayService.class).save(commentReplay);
        return toSuccess();
    }


}
